using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Rensselaer_Event_Finder
{
    public partial class VenueMDI : Form
    {

        #region Variables

        Location curVenue;
        
        #endregion

        #region Public Methods

        public VenueMDI()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Load specified venue onto form
        /// </summary>
        /// <param name="_venue"></param>
        public void load_venue(Location _venue)
        {
            curVenue = _venue;

            string venueCategory = "";

            foreach (Category cat in _venue.Categories)
            {
                venueCategory += cat.Name;
            }
            venueTypeValue.Text = venueCategory;

            venuename.Text = curVenue.Name;
            String addressString = "";
            if (curVenue.StreetAddress1 != null)
            {
                addressString += curVenue.StreetAddress1;
            }
            if (curVenue.StreetAddress2 != null)
            {
                addressString += curVenue.StreetAddress2;
            }
            if (curVenue.State != null)
            {
                addressString = addressString + "\n";
                addressString = addressString + curVenue.State;
            }
            if (curVenue.Zipcode != null)
            {
                addressString = addressString + curVenue.Zipcode;
            }
            addressValue.Text = addressString;

            //load phone
            if (curVenue.Phone != null && curVenue.Phone != "")
            {
                phone.Text = curVenue.Phone;
            }
            else
            {
                phone.Text = "N/A";
            }
            //load website
            if (curVenue.Website != null && curVenue.Website != "")
            {
                website.Enabled = true;
                website.Text = curVenue.Website;
            }
            else
            {
                website.Text = "N/A";
                website.Enabled = false;
            }
            //load description
            if (curVenue.Description != null && curVenue.Description != "")
            {
                longDescript.Text = curVenue.Description;
            }
            else
            {
                longDescript.Text = "N/A";
            }
            //load comments
            commentsBox.Items.Clear();
            for (int i = 0; i < curVenue.Comments.Count; i++)
            {
                commentsBox.Items.Add(curVenue.Comments[i].User.Username + ": " + curVenue.Comments[i].Text);
            }
            //load image
            primaryImage.Image = Globals.loadImageFromURL(_venue.Image_url);
            //load rating
            rating.Text = String.Format("{0:0.##}", Globals.getDoubleAverageRating(_venue)) + "/5";
            //load upcoming events (if exist)
            IList<Event> upEvents = Globals.getEventService().getUpcomingEventsByLocationId(curVenue.Id, 2);
            if (upEvents.Count >= 1)
            {
                event1Panel.Visible = true;
                event1name.Text = upEvents[0].Name;
                event1name.Tag = upEvents[0].Id;
                event1date.Text = upEvents[0].StartTime.ToShortDateString();
                event1time.Text = upEvents[0].StartTime.ToShortTimeString() + "-" +
                                    upEvents[0].EndTime.ToShortTimeString();
                if (upEvents[0].Image_url == "" || upEvents[0].Image_url == null)
                {
                    event1pic.Image = Globals.loadImageFromURL(curVenue.Image_url);
                }
                else
                {
                    event1pic.Image = Globals.loadImageFromURL(upEvents[0].Image_url);
                }

                if (upEvents.Count >= 2)
                {
                    event2Panel.Visible = true;
                    event2name.Text = upEvents[1].Name;
                    event2name.Tag = upEvents[1].Id;
                    event2date.Text = upEvents[1].StartTime.ToShortDateString();
                    event2time.Text = upEvents[1].StartTime.ToShortTimeString() + "-" +
                                        upEvents[1].EndTime.ToShortTimeString();
                    if (upEvents[1].Image_url == "" || upEvents[1].Image_url == null)
                    {
                        event2pic.Image = Globals.loadImageFromURL(curVenue.Image_url);
                    }
                    else
                    {
                        event2pic.Image = Globals.loadImageFromURL(upEvents[1].Image_url);
                    }
                }
                else
                {
                    event2Panel.Visible = false;
                }
            }

            else
            {
                event1Panel.Visible = false;
                event2Panel.Visible = false;
            }

            starRatingControl.SelectedStar = 0;
        }

        #endregion

        #region Form Events

        private void homeBtn_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            MainForm.SelectMDIForm(MainForm.homeFrm);
        }

        private void website_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            string url = ((LinkLabel)sender).Text;
            if (!url.StartsWith("www."))
            {
                url = "www." + url;
            }

            System.Diagnostics.Process.Start(url);
        }

        private void starRatingControl_Click(object sender, EventArgs e)
        {
            if (Globals.CurrentUser == null)
            {
                MessageBox.Show("Must login first", "Error");
                starRatingControl.SelectedStar = 0;
                starRatingControl.Invalidate();
            }
            else
            {
                LocationRating userRating = new LocationRating();
                userRating.Rating = starRatingControl.SelectedStar;
                userRating.User = Globals.CurrentUser;
                userRating.Location = curVenue;
                curVenue.AddRating(userRating);
                Globals.getLocationRatingService().save(userRating);

                //update current rating
                rating.Text = String.Format("{0:0.##}", Globals.getDoubleAverageRating(curVenue)) + "/5";
                foreach (Panel pan in MainForm.allVenues)
                {
                    if ((int)pan.Controls["venueNameLink"].Tag == curVenue.Id)
                    {
                        ((RatingControls.StarRatingControl)pan.Controls["starRating"]).SelectedStar =
                            Globals.getIntAverageRating(curVenue);
                    }
                }
            }

        }

        private void addCommentBtn_Click(object sender, EventArgs e)
        {
            if (newCommentBox.Text != "")
            {
                if (Globals.CurrentUser == null)
                {
                    MessageBox.Show("Must login first", "Error");
                }
                else
                {
                    LocationComment newCmt = new LocationComment();
                    newCmt.User = Globals.CurrentUser;
                    newCmt.Location = curVenue;
                    newCmt.Text = newCommentBox.Text;
                    curVenue.AddComment(newCmt);
                    Globals.getLocationCommentService().save(newCmt);

                    commentsBox.Items.Add(Globals.CurrentUser.Username + ": " + newCommentBox.Text);
                    newCommentBox.Clear();
                }
            }
        }

        private void eventname_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Event load = Globals.getEventService().getById((int)((LinkLabel)sender).Tag);
            MainForm.eventFrm.load_event(load);
            MainForm.SelectMDIForm(MainForm.eventFrm);
        }

        #endregion

    }
}